package com.nation.controller;

import com.nation.domain.User;
import com.nation.domain.VScienceAndTechnologyEducation;
import com.nation.domain.VSocialAndCondition;
import com.nation.service.TechnologyService;
import com.nation.utils.ReadAndStoreFile;
import org.apache.shiro.SecurityUtils;
import org.apache.xpath.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dqd on 2017/11/3.
 */
@Controller
@RequestMapping(value = "/Technology")
public class TechnologyController {
    @Autowired
    TechnologyService TechnologyService;

    /**
     * 科技教育
     * @param map
     * @return
     */
    @RequestMapping(value = "/technology")
    public String development(ModelMap map){
        List<VScienceAndTechnologyEducation> slist=TechnologyService.findAll();
        map.put("slist",slist);
        return "Technology/technology";
    }

    /**
     * 修改页面
     * @param country_code 国家代码
     * @param map
     * @return
     */
    @RequestMapping(value = "/technology_update")
    public String development_update(String country_code,ModelMap map){
        VScienceAndTechnologyEducation technology=TechnologyService.findByCountryCode(country_code);
        System.out.println(technology.getRefrence()+"refer");
        map.put("technology",technology);
        return "Technology/technology-update";
    }

    /**
     * 修改信息
     * @param VScienceAndTechnologyEducation
     * @return
     */
    @RequestMapping(value = "/technology_update.do")
    @ResponseBody
    public String development_pupdate_do(VScienceAndTechnologyEducation VScienceAndTechnologyEducation){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VScienceAndTechnologyEducation.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=TechnologyService.update(VScienceAndTechnologyEducation);
        if(judge)
            return "修改成功";
        else
            return "修改失败";
    }
    @RequestMapping(value = "/technology_add")
    public String tchnology_add(){
        return "Technology/technology-add";
    }
    @RequestMapping(value = "/technology_add.do")
    @ResponseBody
    public String technology_add_do(HttpServletRequest request, @RequestParam(value = "picture",required = false)MultipartFile[] pictures,
                                    String statistics, String conferences, String science_technology_policy,
                                    String technology_agency, String think_tank, String educational_scheme,
                                    String university, String refrence, String country_code){

        VScienceAndTechnologyEducation VScienceAndTechnologyEducation=new VScienceAndTechnologyEducation(statistics,conferences,science_technology_policy,technology_agency,think_tank,educational_scheme,university,refrence,country_code);
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        VScienceAndTechnologyEducation.setRecoder_id(user.getYHZH());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VScienceAndTechnologyEducation.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=null;
        try{
            if (pictures!=null&&pictures.length>0) {//有文件上传
                String path=request.getSession().getServletContext().getResource("/").getPath();
                path += ReadAndStoreFile.TECHENOLOGY;
                System.out.println("path--"+path);

                String picture_path="";
//          将MultipartFile格式转换成File
                for (int i=0;i<pictures.length;i++){
                    String temp=ReadAndStoreFile.save(pictures[i],path);
                    temp=temp.substring(temp.indexOf("nation/"+ReadAndStoreFile.TECHENOLOGY));
                    if (i == (pictures.length-1) || pictures.length == 1){
                        picture_path=temp+picture_path;
                    }else{
                        picture_path+=temp+",";
                    }
                }
                VScienceAndTechnologyEducation.setPicture(picture_path);
            }

            judge=TechnologyService.insert(VScienceAndTechnologyEducation);
        }catch (Exception e){
            e.printStackTrace();
            return "添加失败，出错";
        }
        if(judge)
            return "添加成功";
        else
            return "添加失败 ";
    }
    @RequestMapping(value = "/InfoSearch")
    public String InfoSearch(HttpServletRequest request, ModelMap mmap){
        String recoder_id=request.getParameter("recoder_id");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("recoder_id",recoder_id);
        List<VScienceAndTechnologyEducation> elist=TechnologyService.findByCondition(map);
        mmap.put("slist",elist);
        return "Technology/technology";
    }
    @RequestMapping(value = "/technology_delete")
    @ResponseBody
    public String baseinfo_delete(String country_code){
        Boolean judge=null;
        try{
            judge=TechnologyService.delete(country_code);
        }catch (Exception e){
            e.printStackTrace();
            return "删除失败";
        }
        if(judge)
            return "删除成功";
        else
            return "删除失败";
    }
}
